close all
clear all
clc

filename = 'CurrentMachineLearning.xlsx';
CurrentTable = readtable(filename);
data = table2array(CurrentTable(:,2:end));
n = length(data);
X_train_A = data(1:round(0.7*n),[4:8,19,21,22]);
Y_train_A = data(1:round(0.7*n),9);
X_train_B = data(1:round(0.7*n),[10:14,23:25]);
Y_train_B = data(1:round(0.7*n),26);

X_test_A = data(round(0.7*n):end,[4:8,19,21,22]);
Y_test_A = data(round(0.7*n):end,9);
X_test_B = data(round(0.7*n):end,[10:14,23:25]);
Y_test_B = data(round(0.7*n):end,26);

Default = 0;
if(Default)
    load("CurrentMachineLearning.mat");
else
    BestModel_A = fitcknn(X_train_A, Y_train_A, ...
        'Standardize', true, ...
        'HyperparameterOptimizationOptions',struct('Optimizer','gridsearch', ...
        'KFold',5,...
        'MaxObjectiveEvaluations',20,...
        'Repartition',true, ...
        'NumGridDivisions',100),...
        'OptimizeHyperparameters','all');


    BestModel_B = fitcknn(X_train_B, Y_train_B, ...
        'Standardize', true, ...
        'HyperparameterOptimizationOptions',struct('Optimizer','gridsearch', ...
        'KFold',5,...
        'MaxObjectiveEvaluations',20,...
        'Repartition',true, ...
        'NumGridDivisions',100),...
        'OptimizeHyperparameters','all');
    save("CurrentMachineLearning.mat","BestModel_A","BestModel_B")
end

Y_predict_A = predict(BestModel_A,X_test_A);
accuracy_A = sum(Y_predict_A == Y_test_A)./length(Y_test_A);
Y_predict_A = [Y_predict_A Y_test_A];

Y_predict_B = predict(BestModel_B,X_test_B);
accuracy_B = sum(Y_predict_B == Y_test_B)./length(Y_test_B);
Y_predict_B = [Y_predict_B Y_test_B];




